home *** CD-ROM | disk | FTP | other *** search
/ Aminet 35 / Aminet 35 (2000)(Schatztruhe)[!][Feb 2000].iso / Aminet / game / shoot / ADescentSrc.lha / descent / main / fvi.c < prev    next >
C/C++ Source or Header  |  1998-03-25  |  39KB  |  1,362 lines

  1. /*
  2. THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
  3. SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
  4. END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
  5. ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
  6. IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
  7. SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
  8. FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
  9. CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
  10. AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
  11. COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
  12. */
  13.  
  14. #define NEW_FVI_STUFF 1
  15.  
  16. /*
  17.  * $Source: /usr/CVS/descent/main/fvi.c,v $
  18.  * $Revision: 1.2 $
  19.  * $Author: tfrieden $
  20.  * $Date: 1998/03/25 22:05:15 $
  21.  * 
  22.  * New home for find_vector_intersection()
  23.  * 
  24.  * $Log: fvi.c,v $
  25.  * Revision 1.2  1998/03/25 22:05:15  tfrieden
  26.  * Removed some more warnings (storage class)
  27.  *
  28.  * Revision 1.1.1.1  1998/03/03 15:12:18  nobody
  29.  * reimport after crash from backup
  30.  *
  31.  * Revision 1.1.1.1  1998/02/13  20:20:47  hfrieden
  32.  * Initial Import
  33.  */
  34.  
  35. #pragma off (unreferenced)
  36. static char rcsid[] = "$Id: fvi.c,v 1.2 1998/03/25 22:05:15 tfrieden Exp $";
  37. #pragma on (unreferenced)
  38.  
  39. #include <stdlib.h>
  40. #include <malloc.h>
  41. #include <string.h>
  42. #include <bsd/bsd.h>
  43.  
  44. #include "error.h"
  45. #include "mono.h"
  46.  
  47. #include "inferno.h"
  48. #include "fvi.h"
  49. #include "segment.h"
  50. #include "object.h"
  51. #include "wall.h"
  52. #include "laser.h"
  53. #include "rle.h"
  54. #include "robot.h"
  55. #include "piggy.h"
  56. #include "player.h"
  57.  
  58. extern int Physics_cheat_flag;
  59.  
  60. #define face_type_num(nfaces,face_num,tri_edge) ((nfaces==1)?0:(tri_edge*2 + face_num))
  61.  
  62. int oflow_check(fix a,fix b) {
  63.     return 0;
  64. }
  65.  
  66. #pragma aux oflow_check parm [eax] [ebx] value [eax] modify exact [eax ebx edx] = \
  67.    "cdq"                \
  68.     "xor eax,edx"   \
  69.     "sub eax,edx"   \
  70.     "xchg eax,ebx"  \
  71.    "cdq"                \
  72.     "xor eax,edx"   \
  73.     "sub eax,edx"   \
  74.     "imul ebx"      \
  75.     "sar  edx,15"   \
  76.     "or   dx,dx"    \
  77.     "setnz al"      \
  78.     "movzx eax,al";
  79.  
  80.  
  81. //find the point on the specified plane where the line intersects
  82. //returns true if point found, false if line parallel to plane
  83. //new_pnt is the found point on the plane
  84. //plane_pnt & plane_norm describe the plane
  85. //p0 & p1 are the ends of the line
  86. int find_plane_line_intersection(vms_vector *new_pnt,vms_vector *plane_pnt,vms_vector *plane_norm,vms_vector *p0,vms_vector *p1,fix rad)
  87. {
  88.     vms_vector d,w;
  89.     fix num,den;
  90.  
  91.     vm_vec_sub(&d,p1,p0);
  92.     vm_vec_sub(&w,p0,plane_pnt);
  93.  
  94.     num =  vm_vec_dot(plane_norm,&w);
  95.     den = -vm_vec_dot(plane_norm,&d);
  96.  
  97. //Why does this assert hit so often
  98. //  Assert(num > -rad);
  99.  
  100.     num -= rad;         //move point out by rad
  101.  
  102.     //check for various bad values
  103.  
  104.     if ( (den==0) ||                    //moving parallel to wall, so can't hit it
  105.           (den>0) &&
  106.             ( (num>den) ||              //frac greater than one
  107.              (-num>>15)>=den) ||    //will overflow (large negative)
  108.           (den<0 && num<den))       //frac greater than one
  109.         return 0;
  110.  
  111. //if (num>0) {mprintf(1,"HEY! num>0 in FVI!!!"); return 0;}
  112. //??    Assert(num>=0);
  113. //    Assert(num >= den);
  114.  
  115.     //do check for potenial overflow
  116.     {
  117.         fix k;
  118.  
  119.         if (labs(num)/(f1_0/2) >= labs(den)) {Int3(); return 0;}
  120.         k = fixdiv(num,den);
  121.  
  122.         Assert(k<=f1_0);        //should be trapped above
  123.  
  124. //      Assert(k>=0);
  125.         if (oflow_check(d.x,k) || oflow_check(d.y,k) || oflow_check(d.z,k)) return 0;
  126.         //Note: it is ok for k to be greater than 1, since this might mean
  127.         //that an object with a non-zero radius that moved from p0 to p1 
  128.         //actually hit the wall on the "other side" of p0.
  129.     }
  130.  
  131.     vm_vec_scale2(&d,num,den);
  132.  
  133.     vm_vec_add(new_pnt,p0,&d);
  134.  
  135.     //we should have vm_vec_scale2_add2()
  136.  
  137.     return 1;
  138.  
  139. }
  140.  
  141. typedef struct vec2d {
  142.     fix i,j;
  143. } vec2d;
  144.  
  145. //given largest componant of normal, return i & j
  146. //if largest componant is negative, swap i & j
  147. int ij_table[3][2] =        {
  148.                             {2,1},          //pos x biggest
  149.                             {0,2},          //pos y biggest
  150.                             {1,0},          //pos z biggest
  151.                         };
  152.  
  153. //intersection types
  154. #define IT_NONE 0       //doesn't touch face at all
  155. #define IT_FACE 1       //touches face
  156. #define IT_EDGE 2       //touches edge of face
  157. #define IT_POINT        3       //touches vertex
  158.  
  159. //see if a point in inside a face by projecting into 2d
  160. uint check_point_to_face(vms_vector *checkp,segment *sp, side *s,int facenum,int nv,int *vertex_list)
  161. {
  162.     vms_vector_array *checkp_array;
  163.     vms_vector_array norm;
  164.     vms_vector t;
  165.     int biggest;
  166. ///
  167.     int i,j,edge;
  168.     uint edgemask;
  169.     fix check_i,check_j;
  170.     vms_vector_array *v0,*v1;
  171.  
  172.     #ifdef COMPACT_SEGS
  173.         get_side_normal(sp, s-sp->sides, facenum, (vms_vector *)&norm );
  174.     #else
  175.         memcpy( &norm, &s->normals[facenum], sizeof(vms_vector_array));
  176.     #endif
  177.     checkp_array = (vms_vector_array *)checkp;
  178.  
  179.     //now do 2d check to see if point is in side
  180.  
  181.     //project polygon onto plane by finding largest component of normal
  182.     t.x = labs(norm.xyz[0]); t.y = labs(norm.xyz[1]); t.z = labs(norm.xyz[2]);
  183.  
  184.     if (t.x > t.y) if (t.x > t.z) biggest=0; else biggest=2;
  185.     else if (t.y > t.z) biggest=1; else biggest=2;
  186.  
  187.     if (norm.xyz[biggest] > 0) {
  188.         i = ij_table[biggest][0];
  189.         j = ij_table[biggest][1];
  190.     }
  191.     else {
  192.         i = ij_table[biggest][1];
  193.         j = ij_table[biggest][0];
  194.     }
  195.  
  196.     //now do the 2d problem in the i,j plane
  197.  
  198.     check_i = checkp_array->xyz[i];
  199.     check_j = checkp_array->xyz[j];
  200.  
  201.     for (edge=edgemask=0;edge<nv;edge++) {
  202.         vec2d edgevec,checkvec;
  203.         fix d;
  204.  
  205.         v0 = (vms_vector_array *)&Vertices[vertex_list[facenum*3+edge]];
  206.         v1 = (vms_vector_array *)&Vertices[vertex_list[facenum*3+((edge+1)%nv)]];
  207.  
  208.         edgevec.i = v1->xyz[i] - v0->xyz[i];
  209.         edgevec.j = v1->xyz[j] - v0->xyz[j];
  210.  
  211.         checkvec.i = check_i - v0->xyz[i];
  212.         checkvec.j = check_j - v0->xyz[j];
  213.  
  214.         d = fixmul(checkvec.i,edgevec.j) - fixmul(checkvec.j,edgevec.i);
  215.  
  216.         if (d < 0)                      //we are outside of triangle
  217.             edgemask |= (1<<edge);
  218.     }
  219.  
  220.     return edgemask;
  221.  
  222. }
  223.  
  224.  
  225. //check if a sphere intersects a face
  226. check_sphere_to_face(vms_vector *pnt,segment *sp, side *s,int facenum,int nv,fix rad,int *vertex_list)
  227. {
  228.     vms_vector checkp=*pnt;
  229.     uint edgemask;
  230.  
  231.     //now do 2d check to see if point is in side
  232.  
  233.     edgemask = check_point_to_face(pnt,sp,s,facenum,nv,vertex_list);
  234.  
  235.     //we've gone through all the sides, are we inside?
  236.  
  237.     if (edgemask == 0)
  238.         return IT_FACE;
  239.     else {
  240.         vms_vector edgevec,checkvec;            //this time, real 3d vectors
  241.         vms_vector closest_point;
  242.         fix edgelen,d,dist;
  243.         vms_vector *v0,*v1;
  244.         int itype;
  245.         int edgenum;
  246.  
  247.         //get verts for edge we're behind
  248.  
  249.         for (edgenum=0;!(edgemask&1);(edgemask>>=1),edgenum++);
  250.  
  251.         v0 = &Vertices[vertex_list[facenum*3+edgenum]];
  252.         v1 = &Vertices[vertex_list[facenum*3+((edgenum+1)%nv)]];
  253.  
  254.         //check if we are touching an edge or point
  255.  
  256.         vm_vec_sub(&checkvec,&checkp,v0);
  257.         edgelen = vm_vec_normalized_dir(&edgevec,v1,v0);
  258.         
  259.         //find point dist from planes of ends of edge
  260.  
  261.         d = vm_vec_dot(&edgevec,&checkvec);
  262.  
  263.         if (d+rad < 0) return IT_NONE;                  //too far behind start point
  264.  
  265.         if (d-rad > edgelen) return IT_NONE;    //too far part end point
  266.  
  267.         //find closest point on edge to check point
  268.  
  269.         itype = IT_POINT;
  270.  
  271.         if (d < 0) closest_point = *v0;
  272.         else if (d > edgelen) closest_point = *v1;
  273.         else {
  274.             itype = IT_EDGE;
  275.  
  276.             //vm_vec_scale(&edgevec,d);
  277.             //vm_vec_add(&closest_point,v0,&edgevec);
  278.  
  279.             vm_vec_scale_add(&closest_point,v0,&edgevec,d);
  280.         }
  281.  
  282.         dist = vm_vec_dist(&checkp,&closest_point);
  283.  
  284.         if (dist <= rad)
  285.             return (itype==IT_POINT)?IT_NONE:itype;
  286.         else
  287.             return IT_NONE;
  288.     }
  289.  
  290.  
  291. }
  292.  
  293. //returns true if line intersects with face. fills in newp with intersection
  294. //point on plane, whether or not line intersects side
  295. //facenum determines which of four possible faces we have
  296. //note: the seg parm is temporary, until the face itself has a point field
  297. int check_line_to_face(vms_vector *newp,vms_vector *p0,vms_vector *p1,segment *seg,int side,int facenum,int nv,fix rad)
  298. {
  299.     vms_vector checkp;
  300.     int pli;
  301.     struct side *s=&seg->sides[side];
  302.     int vertex_list[6];
  303.     int num_faces;
  304.     int vertnum;
  305.     vms_vector norm;
  306.  
  307.     #ifdef COMPACT_SEGS
  308.         get_side_normal(seg, side, facenum, &norm );
  309.     #else
  310.         norm = seg->sides[side].normals[facenum];
  311.     #endif
  312.  
  313.     create_abs_vertex_lists(&num_faces,vertex_list,seg-Segments,side);
  314.  
  315.     //use lowest point number
  316.     if (num_faces==2) {
  317.         vertnum = min(vertex_list[0],vertex_list[2]);
  318.     }
  319.     else {
  320.         int i;
  321.         vertnum = vertex_list[0];
  322.         for (i=1;i<4;i++)
  323.             if (vertex_list[i] < vertnum)
  324.                 vertnum = vertex_list[i];
  325.     }
  326.  
  327.     pli = find_plane_line_intersection(newp,&Vertices[vertnum],&norm,p0,p1,rad);
  328.  
  329.     if (!pli) return IT_NONE;
  330.  
  331.     checkp = *newp;
  332.  
  333.     //if rad != 0, project the point down onto the plane of the polygon
  334.  
  335.     if (rad!=0)
  336.         vm_vec_scale_add2(&checkp,&norm,-rad);
  337.  
  338.     return check_sphere_to_face(&checkp,seg,s,facenum,nv,rad,vertex_list);
  339.  
  340. }
  341.  
  342. //returns the value of a determinant
  343. fix calc_det_value(vms_matrix *det)
  344. {
  345.     return  fixmul(det->rvec.x,fixmul(det->uvec.y,det->fvec.z)) -
  346.                 fixmul(det->rvec.x,fixmul(det->uvec.z,det->fvec.y)) -
  347.                 fixmul(det->rvec.y,fixmul(det->uvec.x,det->fvec.z)) +
  348.                 fixmul(det->rvec.y,fixmul(det->uvec.z,det->fvec.x)) +
  349.                 fixmul(det->rvec.z,fixmul(det->uvec.x,det->fvec.y)) -
  350.                 fixmul(det->rvec.z,fixmul(det->uvec.y,det->fvec.x));
  351. }
  352.  
  353. //computes the parameters of closest approach of two lines 
  354. //fill in two parameters, t0 & t1.  returns 0 if lines are parallel, else 1
  355. check_line_to_line(fix *t1,fix *t2,vms_vector *p1,vms_vector *v1,vms_vector *p2,vms_vector *v2)
  356. {
  357.     vms_matrix det;
  358.     fix d,cross_mag2;       //mag squared cross product
  359.  
  360.     vm_vec_sub(&det.rvec,p2,p1);
  361.     vm_vec_cross(&det.fvec,v1,v2);
  362.     cross_mag2 = vm_vec_dot(&det.fvec,&det.fvec);
  363.  
  364.     if (cross_mag2 == 0)
  365.         return 0;           //lines are parallel
  366.  
  367.     det.uvec = *v2;
  368.     d = calc_det_value(&det);
  369.     if (oflow_check(d,cross_mag2))
  370.         return 0;
  371.     else
  372.         *t1 = fixdiv(d,cross_mag2);
  373.  
  374.     det.uvec = *v1;
  375.     d = calc_det_value(&det);
  376.     if (oflow_check(d,cross_mag2))
  377.         return 0;
  378.     else
  379.         *t2 = fixdiv(d,cross_mag2);
  380.  
  381.     return 1;       //found point
  382. }
  383.  
  384. #ifdef NEW_FVI_STUFF
  385. int disable_new_fvi_stuff=0;
  386. #else
  387. #define disable_new_fvi_stuff 1
  388. #endif
  389.  
  390. //this version is for when the start and end positions both poke through
  391. //the plane of a side.  In this case, we must do checks against the edge
  392. //of faces
  393. int special_check_line_to_face(vms_vector *newp,vms_vector *p0,vms_vector *p1,segment *seg,int side,int facenum,int nv,fix rad)
  394. {
  395.     vms_vector move_vec;
  396.     fix edge_t,move_t,edge_t2,move_t2,closest_dist;
  397.     fix edge_len,move_len;
  398.     int vertex_list[6];
  399.     int num_faces,edgenum;
  400.     uint edgemask;
  401.     vms_vector *edge_v0,*edge_v1,edge_vec;
  402.     struct side *s=&seg->sides[side];
  403.     vms_vector closest_point_edge,closest_point_move;
  404.  
  405.     if (disable_new_fvi_stuff)
  406.         return check_line_to_face(newp,p0,p1,seg,side,facenum,nv,rad);
  407.  
  408.     //calc some basic stuff
  409.  
  410.     create_abs_vertex_lists(&num_faces,vertex_list,seg-Segments,side);
  411.     vm_vec_sub(&move_vec,p1,p0);
  412.  
  413.     //figure out which edge(s) to check against
  414.  
  415.     edgemask = check_point_to_face(p0,seg,s,facenum,nv,vertex_list);
  416.  
  417.     if (edgemask == 0)
  418.         return check_line_to_face(newp,p0,p1,seg,side,facenum,nv,rad);
  419.  
  420.     for (edgenum=0;!(edgemask&1);edgemask>>=1,edgenum++);
  421.  
  422.     edge_v0 = &Vertices[vertex_list[facenum*3+edgenum]];
  423.     edge_v1 = &Vertices[vertex_list[facenum*3+((edgenum+1)%nv)]];
  424.  
  425.     vm_vec_sub(&edge_vec,edge_v1,edge_v0);
  426.  
  427.     //is the start point already touching the edge?
  428.  
  429.     //??
  430.  
  431.     //first, find point of closest approach of vec & edge
  432.  
  433.     edge_len = vm_vec_normalize(&edge_vec);
  434.     move_len = vm_vec_normalize(&move_vec);
  435.  
  436.     check_line_to_line(&edge_t,&move_t,edge_v0,&edge_vec,p0,&move_vec);
  437.  
  438.     //make sure t values are in valid range
  439.  
  440.     if (move_t<0 || move_t>move_len+rad)
  441.         return IT_NONE;
  442.  
  443.     if (move_t > move_len)
  444.         move_t2 = move_len;
  445.     else
  446.         move_t2 = move_t;
  447.  
  448.     if (edge_t < 0)     //saturate at points
  449.         edge_t2 = 0;
  450.     else
  451.         edge_t2 = edge_t;
  452.     
  453.     if (edge_t2 > edge_len)     //saturate at points
  454.         edge_t2 = edge_len;
  455.     
  456.     //now, edge_t & move_t determine closest points.  calculate the points.
  457.  
  458.     vm_vec_scale_add(&closest_point_edge,edge_v0,&edge_vec,edge_t2);
  459.     vm_vec_scale_add(&closest_point_move,p0,&move_vec,move_t2);
  460.  
  461.     //find dist between closest points
  462.  
  463.     closest_dist = vm_vec_dist(&closest_point_edge,&closest_point_move);
  464.  
  465.     //could we hit with this dist?
  466.  
  467.     //note massive tolerance here
  468. //  if (closest_dist < (rad*18)/20) {       //we hit.  figure out where
  469.     if (closest_dist < (rad*15)/20) {       //we hit.  figure out where
  470.  
  471.         //now figure out where we hit
  472.  
  473.         vm_vec_scale_add(newp,p0,&move_vec,move_t-rad);
  474.  
  475.         return IT_EDGE;
  476.  
  477.     }
  478.     else
  479.         return IT_NONE;         //no hit
  480.  
  481. }
  482.  
  483. //maybe this routine should just return the distance and let the caller
  484. //decide it it's close enough to hit
  485. //determine if and where a vector intersects with a sphere
  486. //vector defined by p0,p1 
  487. //returns dist if intersects, and fills in intp
  488. //else returns 0
  489. int check_vector_to_sphere_1(vms_vector *intp,vms_vector *p0,vms_vector *p1,vms_vector *sphere_pos,fix sphere_rad)
  490. {
  491.     vms_vector d,dn,w,closest_point;
  492.     fix mag_d,dist,w_dist,int_dist;
  493.  
  494.     //this routine could be optimized if it's taking too much time!
  495.  
  496.     vm_vec_sub(&d,p1,p0);
  497.     vm_vec_sub(&w,sphere_pos,p0);
  498.  
  499.     mag_d = vm_vec_copy_normalize(&dn,&d);
  500.  
  501.     if (mag_d == 0) {
  502.         int_dist = vm_vec_mag(&w);
  503.         *intp = *p0;
  504.         return (int_dist<sphere_rad)?int_dist:0;
  505.     }
  506.  
  507.     w_dist = vm_vec_dot(&dn,&w);
  508.  
  509.     if (w_dist < 0)     //moving away from object
  510.          return 0;
  511.  
  512.     if (w_dist > mag_d+sphere_rad)
  513.         return 0;       //cannot hit
  514.  
  515.     vm_vec_scale_add(&closest_point,p0,&dn,w_dist);
  516.  
  517.     dist = vm_vec_dist(&closest_point,sphere_pos);
  518.  
  519.     if (dist < sphere_rad) {
  520.         fix dist2,rad2,shorten;
  521.  
  522.         dist2 = fixmul(dist,dist);
  523.         rad2 = fixmul(sphere_rad,sphere_rad);
  524.  
  525.         shorten = fix_sqrt(rad2 - dist2);
  526.  
  527.         int_dist = w_dist-shorten;
  528.  
  529.         if (int_dist > mag_d || int_dist < 0) {
  530.             //past one or the other end of vector, which means we're inside
  531.  
  532.             *intp = *p0;        //don't move at all
  533.             return 1;
  534.         }
  535.  
  536.         vm_vec_scale_add(intp,p0,&dn,int_dist);         //calc intersection point
  537.  
  538. //      {
  539. //          fix dd = vm_vec_dist(intp,sphere_pos);
  540. //          Assert(dd == sphere_rad);
  541. //          mprintf(0,"dd=%x, rad=%x, delta=%x\n",dd,sphere_rad,dd-sphere_rad);
  542. //      }
  543.  
  544.  
  545.         return int_dist;
  546.     }
  547.     else
  548.         return 0;
  549. }
  550.  
  551. //$$fix get_sphere_int_dist(vms_vector *w,fix dist,fix rad);
  552. //$$
  553. //$$#pragma aux get_sphere_int_dist parm [esi] [ebx] [ecx] value [eax] modify exact [eax ebx ecx edx] = \
  554. //$$    "mov eax,ebx"       \
  555. //$$    "imul eax"          \
  556. //$$                            \
  557. //$$    "mov ebx,eax"       \
  558. //$$   "mov eax,ecx"        \
  559. //$$    "mov ecx,edx"       \
  560. //$$                            \
  561. //$$    "imul eax"          \
  562. //$$                            \
  563. //$$    "sub eax,ebx"       \
  564. //$$    "sbb edx,ecx"       \
  565. //$$                            \
  566. //$$    "call quad_sqrt"    \
  567. //$$                            \
  568. //$$    "push eax"          \
  569. //$$                            \
  570. //$$    "push ebx"          \
  571. //$$    "push ecx"          \
  572. //$$                            \
  573. //$$    "mov eax,[esi]" \
  574. //$$    "imul eax"          \
  575. //$$    "mov ebx,eax"       \
  576. //$$    "mov ecx,edx"       \
  577. //$$    "mov eax,4[esi]"    \
  578. //$$    "imul eax"          \
  579. //$$    "add ebx,eax"       \
  580. //$$    "adc ecx,edx"       \
  581. //$$    "mov eax,8[esi]"    \
  582. //$$    "imul eax"          \
  583. //$$    "add eax,ebx"       \
  584. //$$    "adc edx,ecx"       \
  585. //$$                            \
  586. //$$    "pop ecx"           \
  587. //$$    "pop ebx"           \
  588. //$$                            \
  589. //$$    "sub eax,ebx"       \
  590. //$$    "sbb edx,ecx"       \
  591. //$$                            \
  592. //$$    "call quad_sqrt"    \
  593. //$$                            \
  594. //$$    "pop ebx"           \
  595. //$$    "sub eax,ebx";
  596. //$$
  597. //$$
  598. //$$//determine if and where a vector intersects with a sphere
  599. //$$//vector defined by p0,p1 
  600. //$$//returns dist if intersects, and fills in intp. if no intersect, return 0
  601. //$$fix check_vector_to_sphere_2(vms_vector *intp,vms_vector *p0,vms_vector *p1,vms_vector *sphere_pos,fix sphere_rad)
  602. //$${
  603. //$$    vms_vector d,w,c;
  604. //$$    fix mag_d,dist,mag_c,mag_w;
  605. //$$    vms_vector wn,dn;
  606. //$$
  607. //$$    vm_vec_sub(&d,p1,p0);
  608. //$$    vm_vec_sub(&w,sphere_pos,p0);
  609. //$$
  610. //$$    //wn = w; mag_w = vm_vec_normalize(&wn);
  611. //$$    //dn = d; mag_d = vm_vec_normalize(&dn);
  612. //$$
  613. //$$    mag_w = vm_vec_copy_normalize(&wn,&w);
  614. //$$    mag_d = vm_vec_copy_normalize(&dn,&d);
  615. //$$
  616. //$$    //vm_vec_cross(&c,&w,&d);
  617. //$$    vm_vec_cross(&c,&wn,&dn);
  618. //$$
  619. //$$    mag_c = vm_vec_mag(&c);
  620. //$$    //mag_d = vm_vec_mag(&d);
  621. //$$
  622. //$$    //dist = fixdiv(mag_c,mag_d);
  623. //$$
  624. //$$dist = fixmul(mag_c,mag_w);
  625. //$$
  626. //$$    if (dist < sphere_rad) {        //we intersect.  find point of intersection
  627. //$$        fix int_dist;                   //length of vector to intersection point
  628. //$$        fix k;                                  //portion of p0p1 we want
  629. //$$//@@        fix dist2,rad2,shorten,mag_w2;
  630. //$$
  631. //$$//@@        mag_w2 = vm_vec_dot(&w,&w);     //the square of the magnitude
  632. //$$//@@        //WHAT ABOUT OVERFLOW???
  633. //$$//@@        dist2 = fixmul(dist,dist);
  634. //$$//@@        rad2 = fixmul(sphere_rad,sphere_rad);
  635. //$$//@@        shorten = fix_sqrt(rad2 - dist2);
  636. //$$//@@        int_dist = fix_sqrt(mag_w2 - dist2) - shorten;
  637. //$$
  638. //$$        int_dist = get_sphere_int_dist(&w,dist,sphere_rad);
  639. //$$
  640. //$$if (labs(int_dist) > mag_d) //I don't know why this would happen
  641. //$$    if (int_dist > 0)
  642. //$$        k = f1_0;
  643. //$$    else
  644. //$$        k = -f1_0;
  645. //$$else
  646. //$$        k = fixdiv(int_dist,mag_d);
  647. //$$
  648. //$$//      vm_vec_scale(&d,k);                     //vec from p0 to intersection point
  649. //$$//      vm_vec_add(intp,p0,&d);         //intersection point
  650. //$$        vm_vec_scale_add(intp,p0,&d,k); //calc new intersection point
  651. //$$
  652. //$$        return int_dist;
  653. //$$    }
  654. //$$    else
  655. //$$        return 0;       //no intersection
  656. //$$}
  657.  
  658. //determine if a vector intersects with an object
  659. //if no intersects, returns 0, else fills in intp and returns dist
  660. fix check_vector_to_object(vms_vector *intp,vms_vector *p0,vms_vector *p1,fix rad,object *obj,object *otherobj)
  661. {
  662.     fix size = obj->size;
  663.  
  664.     if (obj->type == OBJ_ROBOT && Robot_info[obj->id].attack_type)
  665.         size = (size*3)/4;
  666.  
  667.     //if obj is player, and bumping into other player or a weapon of another coop player, reduce radius
  668.     if (obj->type == OBJ_PLAYER && 
  669.             ((otherobj->type == OBJ_PLAYER) ||
  670.             ((Game_mode&GM_MULTI_COOP) && otherobj->type == OBJ_WEAPON && otherobj->ctype.laser_info.parent_type == OBJ_PLAYER)))
  671.         size = size/2;
  672.  
  673.     return check_vector_to_sphere_1(intp,p0,p1,&obj->pos,size+rad);
  674.  
  675. }
  676.  
  677.  
  678. #define MAX_SEGS_VISITED 100
  679. int n_segs_visited;
  680. short segs_visited[MAX_SEGS_VISITED];
  681.  
  682. int fvi_nest_count;
  683.  
  684. //these vars are used to pass vars from fvi_sub() to find_vector_intersection()
  685. int fvi_hit_object; // object number of object hit in last find_vector_intersection call.
  686. int fvi_hit_seg;        // what segment the hit point is in
  687. int fvi_hit_side;       // what side was hit
  688. int fvi_hit_side_seg;// what seg the hitside is in
  689. vms_vector wall_norm;   //ptr to surface normal of hit wall
  690. int fvi_hit_seg2;       // what segment the hit point is in
  691.  
  692. int fvi_sub(vms_vector *intp,int *ints,vms_vector *p0,int startseg,vms_vector *p1,fix rad,short thisobjnum,int *ignore_obj_list,int flags,int *seglist,int *n_segs,int entry_seg);
  693.  
  694. //What the hell is fvi_hit_seg for???
  695.  
  696. //Find out if a vector intersects with anything.
  697. //Fills in hit_data, an fvi_info structure (see header file).
  698. //Parms:
  699. //  p0 & startseg   describe the start of the vector
  700. //  p1                  the end of the vector
  701. //  rad                     the radius of the cylinder
  702. //  thisobjnum      used to prevent an object with colliding with itself
  703. //  ingore_obj          ignore collisions with this object
  704. //  check_obj_flag  determines whether collisions with objects are checked
  705. //Returns the hit_data->hit_type
  706. int find_vector_intersection(fvi_query *fq,fvi_info *hit_data)
  707. {
  708.     int hit_type,hit_seg,hit_seg2;
  709.     vms_vector hit_pnt;
  710.     int i;
  711.  
  712.     Assert(fq->ignore_obj_list != -1);
  713.     Assert((fq->startseg <= Highest_segment_index) && (fq->startseg >= 0));
  714.  
  715.     fvi_hit_seg = -1;
  716.     fvi_hit_side = -1;
  717.  
  718.     fvi_hit_object = -1;
  719.  
  720.     //check to make sure start point is in seg its supposed to be in
  721.     //Assert(check_point_in_seg(p0,startseg,0).centermask==0);  //start point not in seg
  722.  
  723.     // Viewer is not in segment as claimed, so say there is no hit.
  724.     if(!(get_seg_masks(fq->p0,fq->startseg,0).centermask==0)) {
  725.  
  726.         hit_data->hit_type = HIT_BAD_P0;
  727.         hit_data->hit_pnt = *fq->p0;
  728.         hit_data->hit_seg = fq->startseg;
  729.         hit_data->hit_side = hit_data->hit_object = 0;
  730.         hit_data->hit_side_seg = -1;
  731.  
  732.         return hit_data->hit_type;
  733.     }
  734.  
  735.     segs_visited[0] = fq->startseg;
  736.  
  737.     n_segs_visited=1;
  738.  
  739.     fvi_nest_count = 0;
  740.  
  741.     hit_seg2 = fvi_hit_seg2 = -1;
  742.  
  743.     hit_type = fvi_sub(&hit_pnt,&hit_seg2,fq->p0,fq->startseg,fq->p1,fq->rad,fq->thisobjnum,fq->ignore_obj_list,fq->flags,hit_data->seglist,&hit_data->n_segs,-2);
  744.     //!!hit_seg = find_point_seg(&hit_pnt,fq->startseg);
  745.     if (hit_seg2!=-1 && !get_seg_masks(&hit_pnt,hit_seg2,0).centermask)
  746.         hit_seg = hit_seg2;
  747.     else
  748.         hit_seg = find_point_seg(&hit_pnt,fq->startseg);
  749.  
  750. //MATT: TAKE OUT THIS HACK AND FIX THE BUGS!
  751.     if (hit_type == HIT_WALL && hit_seg==-1)
  752.         if (fvi_hit_seg2!=-1 && get_seg_masks(&hit_pnt,fvi_hit_seg2,0).centermask==0)
  753.             hit_seg = fvi_hit_seg2;
  754.  
  755.     if (hit_seg == -1) {
  756.         int new_hit_type;
  757.         int new_hit_seg2=-1;
  758.         vms_vector new_hit_pnt;
  759.  
  760.         //because of code that deal with object with non-zero radius has
  761.         //problems, try using zero radius and see if we hit a wall
  762.  
  763.         new_hit_type = fvi_sub(&new_hit_pnt,&new_hit_seg2,fq->p0,fq->startseg,fq->p1,0,fq->thisobjnum,fq->ignore_obj_list,fq->flags,hit_data->seglist,&hit_data->n_segs,-2);
  764.  
  765.         if (new_hit_seg2 != -1) {
  766.             hit_seg = new_hit_seg2;
  767.             hit_pnt = new_hit_pnt;
  768.         }
  769.     }
  770.  
  771.  
  772. if (hit_seg!=-1 && fq->flags&FQ_GET_SEGLIST)
  773.     if (hit_seg != hit_data->seglist[hit_data->n_segs-1] && hit_data->n_segs<MAX_FVI_SEGS-1)
  774.         hit_data->seglist[hit_data->n_segs++] = hit_seg;
  775.  
  776. if (hit_seg!=-1 && fq->flags&FQ_GET_SEGLIST)
  777.     for (i=0;i<hit_data->n_segs && i<MAX_FVI_SEGS-1;i++)
  778.         if (hit_data->seglist[i] == hit_seg) {
  779.             hit_data->n_segs = i+1;
  780.             break;
  781.         }
  782.  
  783. //I'm sorry to say that sometimes the seglist isn't correct.  I did my
  784. //best.  Really.
  785.  
  786.  
  787. //{ //verify hit list
  788. //
  789. //  int i,ch;
  790. //
  791. //  Assert(hit_data->seglist[0] == startseg);
  792. //
  793. //  for (i=0;i<hit_data->n_segs-1;i++) {
  794. //      for (ch=0;ch<6;ch++)
  795. //          if (Segments[hit_data->seglist[i]].children[ch] == hit_data->seglist[i+1])
  796. //              break;
  797. //      Assert(ch<6);
  798. //  }
  799. //
  800. //  Assert(hit_data->seglist[hit_data->n_segs-1] == hit_seg);
  801. //}
  802.     
  803.  
  804. //MATT: PUT THESE ASSERTS BACK IN AND FIX THE BUGS!
  805. //!!    Assert(hit_seg!=-1);
  806. //!!    Assert(!((hit_type==HIT_WALL) && (hit_seg == -1)));
  807.     //When this assert happens, get Matt.  Matt:  Look at hit_seg2 & 
  808.     //fvi_hit_seg.  At least one of these should be set.  Why didn't 
  809.     //find_new_seg() find something?
  810.  
  811. //  Assert(fvi_hit_seg==-1 || fvi_hit_seg == hit_seg);
  812.  
  813.     Assert(!(hit_type==HIT_OBJECT && fvi_hit_object==-1));
  814.  
  815.     hit_data->hit_type      = hit_type;
  816.     hit_data->hit_pnt       = hit_pnt;
  817.     hit_data->hit_seg       = hit_seg;
  818.     hit_data->hit_side      = fvi_hit_side; //looks at global
  819.     hit_data->hit_side_seg  = fvi_hit_side_seg; //looks at global
  820.     hit_data->hit_object        = fvi_hit_object;   //looks at global
  821.     hit_data->hit_wallnorm  = wall_norm;        //looks at global
  822.  
  823. //  if(hit_seg!=-1 && get_seg_masks(&hit_data->hit_pnt,hit_data->hit_seg,0).centermask!=0)
  824. //      Int3();
  825.  
  826.     return hit_type;
  827.  
  828. }
  829.  
  830. //--unused-- fix check_dist(vms_vector *v0,vms_vector *v1)
  831. //--unused-- {
  832. //--unused--    return vm_vec_dist(v0,v1);
  833. //--unused-- }
  834.  
  835. obj_in_list(int objnum,int *obj_list)
  836. {
  837.     int t;
  838.  
  839.     while ((t=*obj_list)!=-1 && t!=objnum) obj_list++;
  840.  
  841.     return (t==objnum);
  842.  
  843. }
  844.  
  845. int check_trans_wall(vms_vector *pnt,segment *seg,int sidenum,int facenum);
  846.  
  847. int fvi_sub(vms_vector *intp,int *ints,vms_vector *p0,int startseg,vms_vector *p1,fix rad,short thisobjnum,int *ignore_obj_list,int flags,int *seglist,int *n_segs,int entry_seg)
  848. {
  849.     segment *seg;               //the segment we're looking at
  850.     int startmask,endmask;  //mask of faces
  851.     //@@int sidemask;               //mask of sides - can be on back of face but not side
  852.     int centermask;         //where the center point is
  853.     int objnum;
  854.     segmasks masks;
  855.     vms_vector hit_point,closest_hit_point;     //where we hit
  856.     fix d,closest_d=0x7fffffff;                 //distance to hit point
  857.     int hit_type=HIT_NONE;                          //what sort of hit
  858.     int hit_seg=-1;
  859.     int hit_none_seg=-1;
  860.     int hit_none_n_segs=0;
  861.     int hit_none_seglist[MAX_FVI_SEGS];
  862.     int cur_nest_level = fvi_nest_count;
  863.  
  864.     //fvi_hit_object = -1;
  865.  
  866.     /*
  867.     if ( stackavail() < 1024 )                      
  868.     {
  869.         mprintf( (0, "In fvi_sub, stack left is < 1k !\n" ));
  870.         Int3();
  871.     }
  872.     */
  873.  
  874.     if (flags&FQ_GET_SEGLIST)
  875.         *seglist = startseg; 
  876.     *n_segs=1;
  877.  
  878.     seg = &Segments[startseg];
  879.  
  880.     fvi_nest_count++;
  881.  
  882.     //first, see if vector hit any objects in this segment
  883.     if (flags & FQ_CHECK_OBJS)
  884.         for (objnum=seg->objects;objnum!=-1;objnum=Objects[objnum].next)
  885.             if (    !(Objects[objnum].flags & OF_SHOULD_BE_DEAD) &&
  886.                     !(thisobjnum == objnum ) &&
  887.                     (ignore_obj_list==NULL || !obj_in_list(objnum,ignore_obj_list)) &&
  888.                     !laser_are_related( objnum, thisobjnum ) &&
  889.                     !((thisobjnum  > -1)    &&
  890.                         (CollisionResult[Objects[thisobjnum].type][Objects[objnum].type] == RESULT_NOTHING ) &&
  891.                         (CollisionResult[Objects[objnum].type][Objects[thisobjnum].type] == RESULT_NOTHING ))) {
  892.                 int fudged_rad = rad;
  893.  
  894.                 //  If this is a robot:robot collision, only do it if both of them have attack_type != 0 (eg, green guy)
  895.                 if (Objects[thisobjnum].type == OBJ_ROBOT)
  896.                     if (Objects[objnum].type == OBJ_ROBOT)
  897.                         if (!(Robot_info[Objects[objnum].id].attack_type && Robot_info[Objects[thisobjnum].id].attack_type))
  898.                             continue;
  899.  
  900.                 if (Objects[thisobjnum].type == OBJ_ROBOT && Robot_info[Objects[thisobjnum].id].attack_type)
  901.                     fudged_rad = (rad*3)/4;
  902.  
  903.                 //if obj is player, and bumping into other player or a weapon of another coop player, reduce radius
  904.                 if (Objects[thisobjnum].type == OBJ_PLAYER && 
  905.                         ((Objects[objnum].type == OBJ_PLAYER) ||
  906.                         ((Game_mode&GM_MULTI_COOP) &&  Objects[objnum].type == OBJ_WEAPON && Objects[objnum].ctype.laser_info.parent_type == OBJ_PLAYER)))
  907.                     fudged_rad = rad/2; //(rad*3)/4;
  908.  
  909.                 d = check_vector_to_object(&hit_point,p0,p1,fudged_rad,&Objects[objnum],&Objects[thisobjnum]);
  910.  
  911.                 if (d)          //we have intersection
  912.                     if (d < closest_d) {
  913.                         fvi_hit_object = objnum; 
  914.                         Assert(fvi_hit_object!=-1);
  915.                         closest_d = d; 
  916.                         closest_hit_point = hit_point; 
  917.                         hit_type=HIT_OBJECT;
  918.                     }
  919.             }
  920.  
  921.     if (    (thisobjnum > -1 ) && (CollisionResult[Objects[thisobjnum].type][OBJ_WALL] == RESULT_NOTHING ) )
  922.         rad = 0;        //HACK - ignore when edges hit walls
  923.  
  924.     //now, check segment walls
  925.  
  926.     startmask = get_seg_masks(p0,startseg,rad).facemask;
  927.  
  928.     masks = get_seg_masks(p1,startseg,rad);    //on back of which faces?
  929.     endmask = masks.facemask;
  930.     //@@sidemask = masks.sidemask;
  931.     centermask = masks.centermask;
  932.  
  933.     if (centermask==0) hit_none_seg = startseg;
  934.  
  935.     if (endmask != 0) {                             //on the back of at least one face
  936.  
  937.         int side,bit,face;
  938.  
  939.         //for each face we are on the back of, check if intersected
  940.  
  941.         for (side=0,bit=1;side<6 && endmask>=bit;side++) {
  942.             int num_faces;
  943.             num_faces = get_num_faces(&seg->sides[side]);
  944.  
  945.             if (num_faces == 0)
  946.                 num_faces = 1;
  947.  
  948.             // commented out by mk on 02/13/94:: if ((num_faces=seg->sides[side].num_faces)==0) num_faces=1;
  949.  
  950.             for (face=0;face<2;face++,bit<<=1) {
  951.  
  952.                 if (endmask & bit) {            //on the back of this face
  953.                     int face_hit_type;      //in what way did we hit the face?
  954.  
  955.  
  956.                     if (seg->children[side] == entry_seg)
  957.                         continue;       //don't go back through entry side
  958.  
  959.                     //did we go through this wall/door?
  960.  
  961.                     //#ifdef NEW_FVI_STUFF
  962.                     if (startmask & bit)        //start was also though.  Do extra check
  963.                         face_hit_type = special_check_line_to_face( &hit_point,
  964.                                         p0,p1,seg,side,
  965.                                         face,
  966.                                         ((num_faces==1)?4:3),rad);
  967.                     else
  968.                     //#endif
  969.                         //NOTE LINK TO ABOVE!!
  970.                         face_hit_type = check_line_to_face( &hit_point,
  971.                                         p0,p1,seg,side,
  972.                                         face,
  973.                                         ((num_faces==1)?4:3),rad);
  974.  
  975.     
  976.                     if (face_hit_type) {            //through this wall/door
  977.                         int wid_flag;
  978.  
  979.                         //if what we have hit is a door, check the adjoining seg
  980.  
  981.                         if ( (thisobjnum == Players[Player_num].objnum) && (Physics_cheat_flag==0xBADA55) ) {
  982.                             wid_flag = WALL_IS_DOORWAY(seg, side);
  983.                             if (seg->children[side] >= 0 ) 
  984.                                 wid_flag |= WID_FLY_FLAG;
  985.                         } else {
  986.                             wid_flag = WALL_IS_DOORWAY(seg, side);
  987.                         }
  988.  
  989.                         if ((wid_flag & WID_FLY_FLAG) ||
  990.                             ((wid_flag == WID_TRANSPARENT_WALL) && 
  991.                                 ((flags & FQ_TRANSWALL) || (flags & FQ_TRANSPOINT && check_trans_wall(&hit_point,seg,side,face))))) {
  992.  
  993.                             int newsegnum;
  994.                             vms_vector sub_hit_point;
  995.                             int sub_hit_type,sub_hit_seg;
  996.                             vms_vector save_wall_norm = wall_norm;
  997.                             int save_hit_objnum=fvi_hit_object;
  998.                             int i;
  999.  
  1000.                             //do the check recursively on the next seg.
  1001.  
  1002.                             newsegnum = seg->children[side];
  1003.  
  1004.                             for (i=0;i<n_segs_visited && newsegnum!=segs_visited[i];i++);
  1005.  
  1006.                             if (i==n_segs_visited) {                //haven't visited here yet
  1007.                                 int temp_seglist[MAX_FVI_SEGS],temp_n_segs;
  1008.                                 
  1009.                                 segs_visited[n_segs_visited++] = newsegnum;
  1010.  
  1011.                                 if (n_segs_visited >= MAX_SEGS_VISITED)
  1012.                                     goto quit_looking;      //we've looked a long time, so give up
  1013.  
  1014.                                 sub_hit_type = fvi_sub(&sub_hit_point,&sub_hit_seg,p0,newsegnum,p1,rad,thisobjnum,ignore_obj_list,flags,temp_seglist,&temp_n_segs,startseg);
  1015.  
  1016.                                 if (sub_hit_type != HIT_NONE) {
  1017.  
  1018.                                     d = vm_vec_dist(&sub_hit_point,p0);
  1019.  
  1020.                                     if (d < closest_d) {
  1021.  
  1022.                                         closest_d = d; 
  1023.                                         closest_hit_point = sub_hit_point;
  1024.                                         hit_type = sub_hit_type;
  1025.                                         if (sub_hit_seg!=-1) hit_seg = sub_hit_seg;
  1026.  
  1027.                                         //copy seglist
  1028.                                         if (flags&FQ_GET_SEGLIST) {
  1029.                                             int ii;
  1030.                                             for (ii=0;i<temp_n_segs && *n_segs<MAX_FVI_SEGS-1;)
  1031.                                                 seglist[(*n_segs)++] = temp_seglist[ii++];
  1032.                                         }
  1033.  
  1034.                                         Assert(*n_segs < MAX_FVI_SEGS);
  1035.                                     }
  1036.                                     else {
  1037.                                         wall_norm = save_wall_norm;     //global could be trashed
  1038.                                         fvi_hit_object = save_hit_objnum;
  1039.                                     }
  1040.  
  1041.                                 }
  1042.                                 else {
  1043.                                     wall_norm = save_wall_norm;     //global could be trashed
  1044.                                     if (sub_hit_seg!=-1) hit_none_seg = sub_hit_seg;
  1045.                                     //copy seglist
  1046.                                     if (flags&FQ_GET_SEGLIST) {
  1047.                                         int ii;
  1048.                                         for (ii=0;ii<temp_n_segs && ii<MAX_FVI_SEGS-1;ii++)
  1049.                                             hit_none_seglist[ii] = temp_seglist[ii];
  1050.                                     }
  1051.                                     hit_none_n_segs = temp_n_segs;
  1052.                                 }
  1053.                             }
  1054.                         }
  1055.                         else {          //a wall
  1056.                                                                 
  1057.                                 //is this the closest hit?
  1058.     
  1059.                                 d = vm_vec_dist(&hit_point,p0);
  1060.     
  1061.                                 if (d < closest_d) {
  1062.                                     closest_d = d; 
  1063.                                     closest_hit_point = hit_point;
  1064.                                     hit_type = HIT_WALL;
  1065.                                     
  1066.                                     #ifdef COMPACT_SEGS
  1067.                                         get_side_normal(seg, side, face, &wall_norm );
  1068.                                     #else
  1069.                                         wall_norm = seg->sides[side].normals[face]; 
  1070.                                     #endif
  1071.                                     
  1072.     
  1073.                                     if (get_seg_masks(&hit_point,startseg,rad).centermask==0)
  1074.                                         hit_seg = startseg;             //hit in this segment
  1075.                                     else
  1076.                                         fvi_hit_seg2 = startseg;
  1077.  
  1078.                                     //@@else     {
  1079.                                     //@@    mprintf( 0, "Warning on line 991 in physics.c\n" );
  1080.                                     //@@    hit_seg = startseg;             //hit in this segment
  1081.                                     //@@    //Int3();
  1082.                                     //@@}
  1083.  
  1084.                                     fvi_hit_seg = hit_seg;
  1085.                                     fvi_hit_side =  side;
  1086.                                     fvi_hit_side_seg = startseg;
  1087.  
  1088.                                 }
  1089.                         }
  1090.                     }
  1091.                 }
  1092.             }
  1093.         }
  1094.     }
  1095.  
  1096. //      Assert(centermask==0 || hit_seg!=startseg);
  1097.  
  1098. //      Assert(sidemask==0);            //Error("Didn't find side we went though");
  1099.  
  1100. quit_looking:
  1101.     ;
  1102.  
  1103.     if (hit_type == HIT_NONE) {     //didn't hit anything, return end point
  1104.         int i;
  1105.  
  1106.         *intp = *p1;
  1107.         *ints = hit_none_seg;
  1108.         //MATT: MUST FIX THIS!!!!
  1109.         //Assert(!centermask);
  1110.  
  1111.         if (hit_none_seg!=-1) {         ///(centermask == 0)
  1112.             if (flags&FQ_GET_SEGLIST)
  1113.                 //copy seglist
  1114.                 for (i=0;i<hit_none_n_segs && *n_segs<MAX_FVI_SEGS-1;)
  1115.                     seglist[(*n_segs)++] = hit_none_seglist[i++];
  1116.         }
  1117.         else
  1118.             if (cur_nest_level!=0)
  1119.                 *n_segs=0;
  1120.  
  1121.     }
  1122.     else {
  1123.         *intp = closest_hit_point;
  1124.         if (hit_seg==-1)
  1125.             if (fvi_hit_seg2 != -1)
  1126.                 *ints = fvi_hit_seg2;
  1127.             else
  1128.                 *ints = hit_none_seg;
  1129.         else
  1130.             *ints = hit_seg;
  1131.     }
  1132.  
  1133.     Assert(!(hit_type==HIT_OBJECT && fvi_hit_object==-1));
  1134.  
  1135.     return hit_type;
  1136.  
  1137. }
  1138.  
  1139. //--unused-- //compute the magnitude of a 2d vector
  1140. //--unused-- fix mag2d(vec2d *v);
  1141. //--unused-- #pragma aux mag2d parm [esi] value [eax] modify exact [eax ebx ecx edx] = \
  1142. //--unused--    "mov    eax,[esi]"      \
  1143. //--unused--    "imul   eax"                \
  1144. //--unused--    "mov    ebx,eax"            \
  1145. //--unused--    "mov    ecx,edx"            \
  1146. //--unused--    "mov    eax,4[esi]"     \
  1147. //--unused--    "imul   eax"                \
  1148. //--unused--    "add    eax,ebx"            \
  1149. //--unused--    "adc    edx,ecx"            \
  1150. //--unused--    "call   quad_sqrt";
  1151.  
  1152. //--unused-- //returns mag
  1153. //--unused-- fix normalize_2d(vec2d *v)
  1154. //--unused-- {
  1155. //--unused--    fix mag;
  1156. //--unused-- 
  1157. //--unused--    mag = mag2d(v);
  1158. //--unused-- 
  1159. //--unused--    v->i = fixdiv(v->i,mag);
  1160. //--unused--    v->j = fixdiv(v->j,mag);
  1161. //--unused-- 
  1162. //--unused--    return mag;
  1163. //--unused-- }
  1164.  
  1165. #include "textures.h"
  1166. #include "texmerge.h"
  1167.  
  1168. #define cross(v0,v1) (fixmul((v0)->i,(v1)->j) - fixmul((v0)->j,(v1)->i))
  1169.  
  1170. //finds the uv coords of the given point on the given seg & side
  1171. //fills in u & v
  1172. void find_hitpoint_uv(fix *u,fix *v,vms_vector *pnt,segment *seg,int sidenum,int facenum)
  1173. {
  1174.     vms_vector_array *pnt_array;
  1175.     vms_vector_array normal_array;
  1176.     int segnum = seg-Segments;
  1177.     int num_faces;
  1178.     int biggest,ii,jj;
  1179.     side *side = &seg->sides[sidenum];
  1180.     int vertex_list[6],vertnum_list[6];
  1181.     vec2d p1,vec0,vec1,checkp;  //@@,checkv;
  1182.     uvl uvls[3];
  1183.     fix k0,k1;
  1184.     int i;
  1185.  
  1186.     //mprintf(0,"\ncheck_trans_wall  vec=%x,%x,%x\n",pnt->x,pnt->y,pnt->z);
  1187.  
  1188.     //do lasers pass through illusory walls?
  1189.  
  1190.     //when do I return 0 & 1 for non-transparent walls?
  1191.  
  1192.     create_abs_vertex_lists(&num_faces,vertex_list,segnum,sidenum);
  1193.     create_all_vertnum_lists(&num_faces,vertnum_list,segnum,sidenum);
  1194.  
  1195.     //now the hard work.
  1196.  
  1197.     //1. find what plane to project this wall onto to make it a 2d case
  1198.  
  1199.     #ifdef COMPACT_SEGS
  1200.         get_side_normal(seg, sidenum, facenum, (vms_vector *)&normal_array );
  1201.     #else
  1202.         memcpy( &normal_array, &side->normals[facenum], sizeof(vms_vector_array) );
  1203.     #endif
  1204.     biggest = 0;
  1205.  
  1206.     if (abs(normal_array.xyz[1]) > abs(normal_array.xyz[biggest])) biggest = 1;
  1207.     if (abs(normal_array.xyz[2]) > abs(normal_array.xyz[biggest])) biggest = 2;
  1208.  
  1209.     if (biggest == 0) ii=1; else ii=0;
  1210.     if (biggest == 2) jj=1; else jj=2;
  1211.  
  1212.     //2. compute u,v of intersection point
  1213.  
  1214.     //vec from 1 -> 0
  1215.     pnt_array = (vms_vector_array *)&Vertices[vertex_list[facenum*3+1]];
  1216.     p1.i = pnt_array->xyz[ii];
  1217.     p1.j = pnt_array->xyz[jj];
  1218.  
  1219.     pnt_array = (vms_vector_array *)&Vertices[vertex_list[facenum*3+0]];
  1220.     vec0.i = pnt_array->xyz[ii] - p1.i;
  1221.     vec0.j = pnt_array->xyz[jj] - p1.j;
  1222.  
  1223.     //vec from 1 -> 2
  1224.     pnt_array = (vms_vector_array *)&Vertices[vertex_list[facenum*3+2]];
  1225.     vec1.i = pnt_array->xyz[ii] - p1.i;
  1226.     vec1.j = pnt_array->xyz[jj] - p1.j;
  1227.  
  1228.     //vec from 1 -> checkpoint
  1229.     pnt_array = (vms_vector_array *)pnt;
  1230.     checkp.i = pnt_array->xyz[ii];
  1231.     checkp.j = pnt_array->xyz[jj];
  1232.  
  1233.     //@@checkv.i = checkp.i - p1.i;
  1234.     //@@checkv.j = checkp.j - p1.j;
  1235.  
  1236.     //mprintf(0," vec0   = %x,%x  ",vec0.i,vec0.j);
  1237.     //mprintf(0," vec1   = %x,%x  ",vec1.i,vec1.j);
  1238.     //mprintf(0," checkv = %x,%x\n",checkv.i,checkv.j);
  1239.  
  1240.     k1 = -fixdiv(cross(&checkp,&vec0) + cross(&vec0,&p1),cross(&vec0,&vec1));
  1241.     if (vec0.i)
  1242.         k0 = fixdiv(fixmul(-k1,vec1.i) + checkp.i - p1.i,vec0.i);
  1243.     else
  1244.         k0 = fixdiv(fixmul(-k1,vec1.j) + checkp.j - p1.j,vec0.j);
  1245.  
  1246.     //mprintf(0," k0,k1  = %x,%x\n",k0,k1);
  1247.  
  1248.     for (i=0;i<3;i++)
  1249.         uvls[i] = side->uvls[vertnum_list[facenum*3+i]];
  1250.  
  1251.     *u = uvls[1].u + fixmul( k0,uvls[0].u - uvls[1].u) + fixmul(k1,uvls[2].u - uvls[1].u);
  1252.     *v = uvls[1].v + fixmul( k0,uvls[0].v - uvls[1].v) + fixmul(k1,uvls[2].v - uvls[1].v);
  1253.  
  1254.     //mprintf(0," u,v    = %x,%x\n",*u,*v);
  1255. }
  1256.  
  1257. //check if a particular point on a wall is a transparent pixel
  1258. //returns 1 if can pass though the wall, else 0
  1259. int check_trans_wall(vms_vector *pnt,segment *seg,int sidenum,int facenum)
  1260. {
  1261.     grs_bitmap *bm;
  1262.     side *side = &seg->sides[sidenum];
  1263.     int bmx,bmy;
  1264.     fix u,v;
  1265.  
  1266.     Assert(WALL_IS_DOORWAY(seg,sidenum) == WID_TRANSPARENT_WALL);
  1267.  
  1268.     find_hitpoint_uv(&u,&v,pnt,seg,sidenum,facenum);
  1269.  
  1270.     if (side->tmap_num2 != 0)   {
  1271.         bm = texmerge_get_cached_bitmap( side->tmap_num, side->tmap_num2 );
  1272.     } else {
  1273.         bm = &GameBitmaps[Textures[side->tmap_num].index];
  1274.         PIGGY_PAGE_IN( Textures[side->tmap_num] );
  1275.     }
  1276.  
  1277.     if (bm->bm_flags & BM_FLAG_RLE)
  1278.         bm = rle_expand_texture(bm);
  1279.  
  1280.     bmx = ((unsigned) f2i(u*bm->bm_w)) % bm->bm_w;
  1281.     bmy = ((unsigned) f2i(v*bm->bm_h)) % bm->bm_h;
  1282.  
  1283. //note: the line above had -v, but that was wrong, so I changed it.  if 
  1284. //something doesn't work, and you want to make it negative again, you 
  1285. //should figure out what's going on.
  1286.  
  1287.     //mprintf(0," bmx,y  = %d,%d, color=%x\n",bmx,bmy,bm->bm_data[bmy*64+bmx]);
  1288.  
  1289.     return (bm->bm_data[bmy*bm->bm_w+bmx] == TRANSPARENCY_COLOR);
  1290. }
  1291.  
  1292. //new function for Mike
  1293. //note: n_segs_visited must be set to zero before this is called
  1294. int sphere_intersects_wall(vms_vector *pnt,int segnum,fix rad)
  1295. {
  1296.     int facemask;
  1297.     segment *seg;
  1298.  
  1299.     segs_visited[n_segs_visited++] = segnum;
  1300.  
  1301.     facemask = get_seg_masks(pnt,segnum,rad).facemask;
  1302.  
  1303.     seg = &Segments[segnum];
  1304.  
  1305.     if (facemask != 0) {                //on the back of at least one face
  1306.  
  1307.         int side,bit,face;
  1308.  
  1309.         //for each face we are on the back of, check if intersected
  1310.  
  1311.         for (side=0,bit=1;side<6 && facemask>=bit;side++) {
  1312.  
  1313.             for (face=0;face<2;face++,bit<<=1) {
  1314.  
  1315.                 if (facemask & bit) {            //on the back of this face
  1316.                     int face_hit_type;      //in what way did we hit the face?
  1317.                     int num_faces,vertex_list[6];
  1318.  
  1319.                     //did we go through this wall/door?
  1320.  
  1321.                     create_abs_vertex_lists(&num_faces,vertex_list,seg-Segments,side);
  1322.  
  1323.                     face_hit_type = check_sphere_to_face( pnt,seg,&seg->sides[side],
  1324.                                         face,((num_faces==1)?4:3),rad,vertex_list);
  1325.  
  1326.                     if (face_hit_type) {            //through this wall/door
  1327.                         int child,i;
  1328.  
  1329.                         //if what we have hit is a door, check the adjoining seg
  1330.  
  1331.                         child = seg->children[side];
  1332.  
  1333.                         for (i=0;i<n_segs_visited && child!=segs_visited[i];i++);
  1334.  
  1335.                         if (i==n_segs_visited) {                //haven't visited here yet
  1336.  
  1337.                             if (!IS_CHILD(child))
  1338.                                 return 1;
  1339.                             else {
  1340.  
  1341.                                 if (sphere_intersects_wall(pnt,child,rad))
  1342.                                     return 1;
  1343.                             }
  1344.                         }
  1345.                     }
  1346.                 }
  1347.             }
  1348.         }
  1349.     }
  1350.  
  1351.     return 0;
  1352. }
  1353.  
  1354. //Returns true if the object is through any walls
  1355. int object_intersects_wall(object *objp)
  1356. {
  1357.     n_segs_visited = 0;
  1358.  
  1359.     return sphere_intersects_wall(&objp->pos,objp->segnum,objp->size);
  1360. }
  1361.  
  1362.